package com.warrantyhotel.android.ui.credits;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.warrantyhotel.android.R;
import com.warrantyhotel.android.adapter.FreeCreditAdapter;
import com.warrantyhotel.android.db.QueriesCodes;
import com.warrantyhotel.android.db.WarrantyHotelDBSchema.GetFreeTable;
import com.warrantyhotel.android.model.Keys;
import com.warrantyhotel.android.service.UpdateIntentService;
import com.warrantyhotel.android.util.DetachableResultReceiver;
import com.warrantyhotel.android.util.NotifyingAsyncQueryHandler;
import com.warrantyhotel.android.util.ToastUtil;
import com.warrantyhotel.android.util.DetachableResultReceiver.Receiver;
import com.warrantyhotel.android.util.NotifyingAsyncQueryHandler.AsyncQueryListener;

public class FreeCredits extends Activity implements AsyncQueryListener, Receiver, OnItemClickListener {

	private ListView listView;
	private NotifyingAsyncQueryHandler mHandler;
	private DetachableResultReceiver receiver;
	private FreeCreditAdapter mAdapter;
	private ProgressDialog progress;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.free_credits);

		listView = (ListView) findViewById(R.id.free_credits_list);
		listView.setOnItemClickListener(this);

		mHandler = new NotifyingAsyncQueryHandler(getContentResolver(), this);
		receiver = new DetachableResultReceiver(new Handler());
		receiver.setReceiver(this);

		RetrieveFreeFromDB();

		Intent testIntent = new Intent(Intent.ACTION_SYNC, null, this, UpdateIntentService.class);
		testIntent.putExtra(UpdateIntentService.EXTRA_STATUS_RECEIVER, receiver);
		testIntent.putExtra(UpdateIntentService.EXTRA_REQUEST_CODE, QueriesCodes.FREE_CREDITS);
		startService(testIntent);
	}

	private void RetrieveFreeFromDB() {
		String[] projectionFree = { GetFreeTable._ID, GetFreeTable.ACTION_TEXT, GetFreeTable.ACTION_TITLE, GetFreeTable.THUMBNAIL_LINK,
				GetFreeTable.AWARD_CREDITS, GetFreeTable.AWARD_UPLOADS };
		mHandler.startQuery(QueriesCodes.FREE_CREDITS, GetFreeTable.CONTENT_URI, projectionFree, null, null, null);
	}

	private void RetrieveFreeFromDBByID(String freeID) {
		String[] projectionFree = { GetFreeTable._ID, GetFreeTable.GET_FREE_ID, GetFreeTable.ACTION_TEXT, GetFreeTable.ACTION_TITLE,
				GetFreeTable.THUMBNAIL_LINK, GetFreeTable.AWARD_CREDITS, GetFreeTable.AWARD_UPLOADS };
		mHandler.startQuery(QueriesCodes.FREE_CREDITS_ID, GetFreeTable.CONTENT_URI, projectionFree, GetFreeTable._ID + " = " + freeID, null, null);
	}

	@Override
	public void onReceiveResult(int resultCode, Bundle resultData) {
		switch (resultCode) {
		case UpdateIntentService.STATUS_RUNNING:
			// do nothing
			break;
		case UpdateIntentService.STATUS_ERROR:
			if (progress != null)
				progress.dismiss();
			ToastUtil.showText(getApplicationContext(), resultData.getString(UpdateIntentService.ERROR_MESSAGE));
			break;
		case UpdateIntentService.STATUS_FINISHED:
			if (progress != null)
				progress.dismiss();
			RetrieveFreeFromDB();
			break;
		}
	}

	@Override
	public void onQueryComplete(int token, Object cookie, Cursor cursor) {
		switch (token) {
		case QueriesCodes.FREE_CREDITS:
			if (cursor != null) {
				startManagingCursor(cursor);
				if (cursor.getCount() == 0 && progress == null)
					progress = ProgressDialog.show(this, getString(R.string.loading), getString(R.string.please_wait), true, false);

				if (mAdapter == null) {
					mAdapter = new FreeCreditAdapter(getApplicationContext(), cursor);
					listView.setAdapter(mAdapter);
				} else {
					mAdapter.changeCursor(cursor);
					mAdapter.notifyDataSetChanged();
				}

			}
			break;
		case QueriesCodes.FREE_CREDITS_ID:
			if (cursor != null) {
				if (cursor.moveToFirst()) {
					String free_id = cursor.getString(cursor.getColumnIndex(GetFreeTable.GET_FREE_ID));
					Intent intent = new Intent(getApplicationContext(), FreeCreditsDetail.class);
					intent.putExtra(Keys._free_credit_id, free_id);
					startActivity(intent);
					cursor.close();
				}
			}
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View view, int position, long id) {

		RetrieveFreeFromDBByID(String.valueOf(id));

	}

}
